package com.imooc.coupon.controller;

import com.alibaba.fastjson.JSON;
import com.imooc.coupon.entity.CouponTemplate;
import com.imooc.coupon.exception.CouponException;
import com.imooc.coupon.service.IBuildTemplateService;
import com.imooc.coupon.service.ITemplateBaseService;
import com.imooc.coupon.vo.CouponTemplateSDK;
import com.imooc.coupon.vo.TemplateRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 优惠劵模板功能相关的控制器
 *
 * @Author Administrator
 * @Date 2021/10/27 20:42
 * @Version 1.0
 */
@Slf4j
@RestController
public class CouponTemplateController {

    @Autowired
    private IBuildTemplateService buildTemplateService;
    @Autowired
    private ITemplateBaseService templateBaseService;

    /**
     * 构建优惠劵
     *127.0.0.1:7001/coupom-template/template/build
     * @param request 127.0.0.1:9000/coupom-template/template/build
     * @return
     * @throws CouponException
     */
    @PostMapping("/template/build")
    public CouponTemplate buildTemplate(@RequestBody TemplateRequest request) throws CouponException {
        log.info("request is {}:", JSON.toJSONString(request));
        return buildTemplateService.buildTemplate(request);
    }

    /**
     * 构造优惠券详情 127.0.0.1:7001/coupom-template/template/info
     * @param request
     * @return
     * @throws CouponException
     */
    @GetMapping("/template/info")
    public CouponTemplate buildTemplateInfo(@RequestParam("id") Integer id) throws CouponException {
        log.info("request is {}:", id);
        return templateBaseService.buildTemplateInfo(id);
    }

    /**
     * 查询所有的可用的优惠券模板 127.0.0.1:7001/coupom-template/template/sdk/all
     * @return
     * @throws CouponException
     */
    @GetMapping("/template/sdk/all")
    public List<CouponTemplateSDK> findAllUsAbleTemplate() throws CouponException {
        return templateBaseService.findAllUsAbleTemplate();
    }
    /**
     * 查询所有的可用的优惠券模板 127.0.0.1:7001/coupom-template/template/sdk/infos
     * @return
     * @throws CouponException
     */
    @GetMapping("/template/sdk/infos")
    public Map<Integer, CouponTemplateSDK> findAllUsAbleTemplate(Collection<Integer> ids) throws CouponException {
        return templateBaseService.findIds2CopontemplateSDK(ids);
    }

}
